// pages/repay/repay.js
Page({
  data: {
    goodsList:[],
    address:{},//地址
    ids:[],
    reorder:[],//未支付的订单
    address_id:"",
    order_id:"",//订单的id
    amount:0,
    token:"",
    appid: "",
    timeStamp: "",
    nonce_str: "",
    package: "",
    signType: "MD5",
    paySign: "" 
  },
  onLoad(option){
    // console.log(option)
    let {order_id} = option
    //设置订单的id
    this.setData({
      order_id:order_id
    })
    this.getToken()
  },
  getToken(){
    wx.getStorage({
      key: 'token',
      success:(res)=>{
        //设置token 
        this.setData({token:res.data})
        //获取订单列表
        this.getOrderList()
      }
    })
  },
  getSelectGoodList(){
    wx.request({
      url:"http://api_devs.wanxikeji.cn/api/shoppingCarList",
      method: "POST",
      data:{
        token:this.data.token
      },
      success:(res)=>{
        let arr = []
        let { indexArr } = this.data
        for (let i = 0; i < indexArr.length; i++) {
          arr.push(res.data.data.data[indexArr[i]])
        }
        for (let i = 0; i < arr.length; i++) {
          this.data.ids.push(arr[i].shopping_car_id)
        }
        this.setData({
          goodsList: arr,
          ids:this.data.ids
        })
        this.countAmount()
      }
    })
  },
  //获取订单列表
  getOrderList(){
    wx.request({
      url:"http://api_devs.wanxikeji.cn/api/orderList",
      method:"POST",
      data:{
        token:this.data.token,
        //请求所有未支付的订单 
        status:1
      },
      success:(res)=>{
        let reorderArr = res.data.data.data//未支付订单列表
        const {order_id} = this.data
        let reorder = reorderArr.filter(item=>item.order_id==order_id)
        console.log(reorder)
        this.setData({
          reorder,
          goodsList:reorder[0].childern,
          address_id:reorder[0].address_id
        })
        this.getAddress()
        this.countAmount()
      }
    })
  },
  //获取地址
  getAddress(){
    wx.request({
      url:"http://api_devs.wanxikeji.cn/api/userAddressList",
      method:"POST",
      data:{
        token:this.data.token
      },
      success:(res)=>{
        console.log(res)
        let addressArr = res.data.data
        const {address_id} = this.data
        let address = addressArr.filter(item=>item.address_id==address_id)
        console.log(address)
        this.setData({
          address:address[0]
        })
      }
    })
  },
  countAmount() {
    let result = this.data.goodsList.reduce((pre, next) => {
      return pre + next.price * next.num
    }, 0)
    if (result == 0) {
      this.setData({
        amount: result
      })
    } else {
      this.setData({
        amount: result.toFixed(2)
      })
    }
  },
  repay(){
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/rePayOrder',
      method:'POST',
      data:{
        token:this.data.token,
        order_id:this.data.order_id
      },
      success:(res)=>{
      //   console.log(res)
        let result = res.data.data
        this.appid = result.appid
        this.timeStamp = result.timeStamp
        this.nonce_str = result.nonce_str
        this.package = `prepay_id=${result.prepay_id}`
        this.paySign = result.paySign
        wx.requestPayment({
          timeStamp: this.timeStamp,
          nonceStr: this.nonce_str,
          package: this.package,
          signType: 'MD5',
          paySign: this.paySign,
          success:()=>{
            // console.log(res)
            wx.showToast({
              icon: 'success',
              title: '支付成功',
              duration:1000
            })
            setTimeout(()=>{
            wx.reLaunch({ url: '/pages/shopingcar/shopingcar' })
            },1000)
          },
          fail:()=>{
            // let {indexArr,address} = this.data
            // console.log("选中商品下标的数组",indexArr)
            // console.log("地址",address)
            wx.showToast({
              icon: 'error',
              title: '支付失败',
              duration:1000
            })
          }
        })
      },
      fail:(res)=>{
        console.log(res)
      }
    })
  }
})